package org.skywind.WindSearch.util;

import java.util.List;
import java.util.Map;
import org.skywind.WindSearch.OperationHost;
import org.skywind.WindSearch.SearchManager;
import org.skywind.WindSearch.SearchResults;
import org.skywind.WindSearch.Beans.KeyBean;
import org.skywind.WindSearch.dao.SearchAction;
import org.skywind.tools.*;

/**
 * 
 * 
 * @author skyWind
 * 
 */
public class Guest<T> {
	private int currPage = 1;
	private int pageSize = 20;
	private String address;
	private String host;
	private String language;
	private SearchResults result;
	private SearchManager sm;
	private SearchAction sa;
	private T status;
	
	public Guest(SearchManager sm, SearchAction sa, int pageSize, String addr) {
		this.sm = sm;
		this.pageSize = pageSize;
		this.sa = sa;
		assign(addr);
	}

	/**
	 * 根据用户IP指派远程业务主机
	 * @param address
	 * @return
	 */
	public boolean assign(String address){
		this.address = address;
		this.host = sm.getHostReflect().assignHost(address);
		
		System.out.println(address);
		System.out.println(host);
		System.out.println(sm.getOprationPool());
		System.out.println(sm.getOprationPool().get(host));
		
		this.language = sm.getOprationPool().get(host).getDefaultLanguage();
		
		Log.println("[Guest]assign host from address: optionHost="+host+",\tlanguage="+language+",\tIP="+address);
		return this.host != null;
	}

	public OperationHost getHost(){
		return sm.getOprationPool().get(host);
	}
	
	public Map getLanguage(){
		return (Map)getHost().getObjectPool().get(language);
	}
	
	public String getKeys(){
		return result!=null?result.getKey()!=null?result.getKey():"":"";
	}
	
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	public T getStatus() {
		return status;
	}

	public void setStatus(T status) {
		this.status = status;
	}
	
	public SearchResults getResult() {
		return result;
	}

	public List getPageList(int page) {
		return this.getResult().getPageList(page, pageSize);
	}

	public List getCurrPageList() {
		return getPageList(currPage);
	}

	public boolean isSucceed() {
		return result.getCount() > 0;
	}

	public boolean doSearch(String index, String key, Object... args) {
		this.result = new SearchResults(index,key,args);
		
		if(args==null || args.length>0 && args[0].equals("")){
			return sm.doSearch(result);//搜索所有远程业务主机
		}
		
		return sm.doSearch(result,this.host);//搜索指定远程业务主机;
	}

	public long getCountAll() {
		return result.count();
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize<=0?1:pageSize;
	}

	/**
	 * 锟斤拷锟矫碉拷前页锟斤拷
	 * 
	 * @param currPage
	 */
	public void setCurrPage(int currPage) {
		this.currPage = currPage<=0?1:currPage;
	}

	/**
	 * 锟斤拷取锟斤拷前页锟斤拷
	 * 
	 * @return
	 */
	public int getCurrPage() {
		return currPage;
	}

	/**
	 * 锟斤拷取锟斤拷锟绞憋拷锟斤拷式锟斤拷锟街凤拷
	 * 
	 * @return
	 */
	public String getExpendTimeStr() {
		return InfoFormat.toTimeStr(this.result.getTime());
	}

	/**
	 * 锟斤拷取锟斤拷页锟斤拷
	 * 
	 * @return
	 */
	public int getPageCount() {
		int size = result!=null?result.getList().size():0;
		int count = size / pageSize;
		count += size % pageSize > 0 ? 1 : 0;
		return count;
	}

	public int getListSize(){
		return this.getResult().getList().size();
	}
	
	/**
	 * 锟斤拷取锟斤拷锟斤拷丶锟斤拷锟�
	 * 
	 * @return
	 */
	public String getSearchKey() {
		return result.getKey();
	}

	public String getType() {
		return result==null?"":result.getIndex();
	}
	
	public List<KeyBean>  getCorrelations(){
		return sa.getCorrelationKeys(result.getKey());
	}

	public String getHostHandle() {
		return host;
	}

	public void setHostHandle(String hostHandle) {
		this.host = hostHandle;
	}

	public String getLanguageHandle() {
		return language;
	}

	public void setLanguageHandle(String languageHandle) {
		this.language = languageHandle;
	}
}
